package cn.cd.anStudio.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import cn.cd.anStudio.MyBusCommonBase;
import android.text.format.Time;
import android.util.Log;

public class MyBusCommonUtil {
	
	private static final String TAG = "MyBusCommonUtil";
	
	public static boolean isFirstLogin() {
		
		if (null == MyBusCommonBase.getVersion())
			return true;

		return MyBusCommonBase.getVersion().isEmpty();
	}
	
	public static Time getTimeFromString (String timeInString) {

		Time validTime = new Time();

		if (MyBusCommonBase.DEBUG)
			Log.v(TAG, "getTimeFromString(" + timeInString + ")");
		
		// Remove blank within string
		timeInString.replaceAll(" ", "");
		String[] mySplittedTime = timeInString.split(":");
		
		validTime.hour = Integer.parseInt(mySplittedTime[0]);
		validTime.minute = Integer.parseInt(mySplittedTime[1]);

		return validTime;
	}
	
	public static String formatPlainString(String str, String encode) {
		String tmp = str.replaceAll("\\n", "").replaceAll("\\r", "");
		String formattedStr = tmp;
		
		if (null != encode) {
			try {
				formattedStr = URLEncoder.encode(tmp, encode);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return formattedStr;
	}
	
	public static int scheduleQueryInterInMilliSecond(int distance) {
		
		final int perSecond = 1000;
		int retFactor = 30;
		
		if ((distance > 0) && (distance <= 2)) {
			retFactor = 10;
		}
		
		if (distance < 0) {
			retFactor = 60;
		}
		
		if (distance > 2) {
			retFactor = 120;
		}
		
		return retFactor * perSecond;
	}
}